# Functions for the model where juveniles still migrate but adults experience predation
Patchads <- function(p, s1=s1, J=J, fx=fx, pred=pred) {
## p is the proportion of juveniles migrating to the high predation patch.
A <- matrix(c(0, fx[1], 0, 0,
J[1]*p,s1[1]*pred,J[2]*p,0,
0, 0, 0,fx[2],
J[1]*(1-p),0, J[2]*(1-p),s1[2]), nrow=4, ncol=4, byrow=TRUE)
return(lambda(A))
}
PatchAstochAds = function(p, states, fx, n0, npatch, nstg, tf, P, pred) {
# start all patches off with a good year
L= numeric(tf)
state = c(1,1)
# k= number of possible states
k=dim(P)[1]
Nt = n0 # starting population
for(i in 1:(tf-1)) {
# determine the environmental state for each patch
state[1] = sample(1:k,size=1, prob = P[,state[1]])
state[2] = sample(1:k,size=1, prob = P[,state[2]])
# pull out the parameters so you can put them in a matrix
st = c(states[state[1],], states[state[2],])
J = st[c(1, 3)]
s1= st[c(2, 4)]
A <- matrix(c(0, fx[1], 0, 0,
J[1]*p,s1[1]*pred,J[2]*p,0,
0, 0, 0,fx[2],
J[1]*(1-p),0, J[2]*(1-p),s1[2]), nrow=4, ncol=4, byrow=TRUE)
L[i] = (lambda(A))
Nt= A %*% Nt
# run model through time
# if (sum(Nt)<0.001) Nt = rep(0, (nstg*npatch))
}
# NAds = Nt[3,]+ Nt[6,]
# pop = cbind(NAds, Nt[3,], Nt[6,], L)
return(L)
}
fooAds = function(p, pred, states1, fx=c(150,150)) {
lams = PatchAstochAds(p, states=states1, fx, n0, npatch, nstg, tf, P, pred)
avelam = sum(log(lams[lams!=0]))/length(lams[lams!=0])
}
Getmat2 <- function( p, state, fx, n0, npatch, nstg, P, pred) {
# determine the environmental state for each patch
k=dim(P)[1]
Lsd = c(0,0)
state[1] = sample(1:k,size=1, prob = P[,state[1]])
state[2] = sample(1:k,size=1, prob = P[,state[2]])
# pull out the parameters so you can put them in a matrix
st = c(states[state[1],], states[state[2],])
J= st[c(1, 3)]
s1= st[c(2, 4)]
A <- matrix(c(0, fx[1], 0, 0,
J[1]*p,s1[1]*pred,J[2]*p,0,
0, 0, 0,fx[2],
J[1]*(1-p),0, J[2]*(1-p),s1[2]), nrow=4, ncol=4, byrow=TRUE)
return(A)
}
bigrun2 = function(tf, p1, pred1) {
As <- replicate(tf, Getmat2(p=p1, state, fx, n0, npatch, nstg, P, pred=pred1))
StochSens(As)$elasticities
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.